// 创建页面
import 'package:flutter/material.dart';
import 'package:flutter_billi/navgator/bottom_navigator.dart';
import 'package:flutter_billi/page/detail_page.dart';
import 'package:flutter_billi/page/login_page.dart';
import 'package:flutter_billi/page/registrion_page.dart';

pageWarp(Widget child) {
  return MaterialPage(child: child, key: ValueKey(child.hashCode));
}

// 声明一个 监听 路由变化 的类型
// 参数1 当前页面
// 参数2 上一个页面
typedef RouteChangeListener(RouteStatusInfo current, RouteStatusInfo pre);
// 定义路由状态
enum RouteStatus { login, home, detail, registration, unkown }

// 获取 routeStatus 在 路由栈中的位置
int getPageIndex(List<MaterialPage> pages, RouteStatus routeStatus) {
  for (int i = 0; i < pages.length; i++) {
    MaterialPage page = pages[i];
    if (getRouteState(page) == routeStatus) {
      return i;
    }
  }
  return -1;
}

// 定义获取路由状态
RouteStatus getRouteState(MaterialPage page) {
  if (page.child is LoginPage) {
    return RouteStatus.login;
  } else if (page.child is RegistrationPage) {
    return RouteStatus.registration;
  } else if (page.child is BottomNavigator) {
    return RouteStatus.home;
  } else if (page.child is VideoDetailPage) {
    return RouteStatus.detail;
  } else {
    return RouteStatus.unkown;
  }
}

// 路由信息
class RouteStatusInfo {
  final RouteStatus routeStatus;
  final Widget page;

  RouteStatusInfo(this.routeStatus, this.page);
}

// 监听路由页面变化
// 感知页面是否被压后台
class HiNavigator extends _RouteJumpListener {
  static HiNavigator _instance;
  HiNavigator._();

  RouteJumpListener _routeJump;

  List<RouteChangeListener> _listeners = [];

  RouteStatusInfo _current;

  // 首页底部tab
  RouteStatusInfo _bottomTab;

  // 提供一个 注册 路由的跳转 逻辑
  void registerRouteJump(RouteJumpListener routeJumpListener) {
    this._routeJump = routeJumpListener;
  }

  static HiNavigator getInstance() {
    if (_instance == null) {
      // return _instance;
      _instance = HiNavigator._();
    }
    return _instance;
  }

  // 注册监听者
  void addListener(RouteChangeListener routeChangeListener) {
    if (!_listeners.contains(routeChangeListener)) {
      _listeners.add(routeChangeListener);
    }
  }

  //监听者 移除
  void removeListener(RouteChangeListener listener) {
    _listeners.remove(listener);
  }

  // 通知路由页面变化
  void notify(List<MaterialPage> currentPages, List<MaterialPage> prePages) {
    if (currentPages == prePages) return;
    var current = RouteStatusInfo(getRouteState(currentPages.last), currentPages.last.child);
    _notify(current);
  }

  /// 添加 首页底部切换的监听
  void BottomTabChange(int index, Widget page) {
    _bottomTab = RouteStatusInfo(RouteStatus.home, page);
    _notify(_bottomTab);
  }

  void _notify(RouteStatusInfo current) {
    print('hi_navigator:current:${current.page}');
    print('hi_navigator:pre:${_current?.page}');

    if (current is BottomNavigator && _bottomTab != null) {
      current = _bottomTab;
    }
    _listeners.forEach((listener) {
      listener(current, _current);
    });
    _current = current;
  }

  @override
  void onJumpTo(RouteStatus routeStatus, {Map args}) {
    _routeJump.onJumpTo(routeStatus, args: args);
  }
}

// 定义抽象类 跳转
abstract class _RouteJumpListener {
  void onJumpTo(RouteStatus routeStatus, {Map args});
}

typedef OnJumpTo = void Function(RouteStatus routeStatus, {Map args});

class RouteJumpListener {
  final OnJumpTo onJumpTo;

  RouteJumpListener({this.onJumpTo});
}
